<?php
// 连接liaoliao数据库
class mysql{
	private $conn;
	public function __construct($dbname='xzpf',$h='localhost',$u='root',$p='',$char='UTF8'){
		$this->conn=$this->connect($h,$u,$p);
		if (!$this->conn) {
			return false;
		}
		$this->useDb($dbname);
		$this->setChar($char);
	}
	protected function connect($h,$u,$p){
		if (!mysql_connect($h,$u,$p)) {
			return false;
		}
		return mysql_connect($h,$u,$p);
	}
	protected function useDb($dbname){
		$sql="use ".$dbname;
		return $this->query($sql);
	}
	protected function setChar($char){
		$sql="set names ".$char;
		return $this->query($sql);
	}
	public function query($sql){
		$q=mysql_query($sql,$this->conn);
		if (!$q) {
			return false;
		}
		return $q;
	}

	public function getAll($sql){
		$list=array();
		$a=$this->query($sql);
		if (!$a) {
			return false;
		}
		while ($b=mysql_fetch_assoc($a)) {
			$list[]=$b;
		}
		return $list;
	}
	public function getRow($sql) {  
        $rs = $this->query($sql);  
        return mysql_fetch_assoc($rs);  
    }
	public function getOne($sql){
		$a=$this->query($sql);
		if (!$a) {
			return false;
		}
		$b=mysql_fetch_row($a);
		return $b[0];
	}

	public function affected_rows(){
		return mysql_affected_rows($this->conn);
	}
	public function autoExecute($table,$arr,$mode='insert',$where = ' where 1 limit 1') {  
        if(!is_array($arr)) {  
            return false;  
        }
       //update 拼接  
        if($mode == 'update') {  
            $sql = 'update ' . $table .' set ';  
            foreach($arr as $k=>$v) {  
                $sql .= $k . "='" . $v ."',";  
            }  
            $sql = rtrim($sql,',');  
            $sql .= $where;  
            return $this->query($sql);  
        }  
        //insert 拼接  
        $sql = 'insert into ' . $table . ' (' . implode(',',array_keys($arr)) . ')';  
        $sql .= ' values (\'';  
        $sql .= implode("','",array_values($arr));  
        $sql .= '\')';  
        return $this->query($sql);
    }

    public function insert_id(){
    	return mysql_insert_id();
    }

	public function close(){
		mysql_close($this->conn);
	}

}
// 连接liaoliao数据库 end
$mysql=new mysql();
date_default_timezone_set('Asia/Shanghai');
session_start();
?>
